package cn.toesbieya.jxc.service.sys;

import cn.toesbieya.jxc.mapper.SysTypeMapper;
import cn.toesbieya.jxc.model.entity.SysType;
import cn.toesbieya.jxc.model.vo.R;
import cn.toesbieya.jxc.model.vo.result.PageResult;
import cn.toesbieya.jxc.model.vo.search.TypeSearch;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xiao_jie
 * @date 2023/4/14 19:17
 * @describe this is a text
 */

@Service
public class SysTypeService {

    @Resource
    private SysTypeMapper mapper;

    public List<SysType> get() {
        return mapper.selectList(
                Wrappers.lambdaQuery(SysType.class)
                        .eq(SysType::isEnable, true)
        );
    }

    public SysType getTypeById(Integer id) {
        return id == null ? null : mapper.selectById(id);
    }

    public PageResult<SysType> search(TypeSearch vo) {
        Integer id = vo.getId();
        String name = vo.getName();
        String came = vo.getCname();
        Boolean status = vo.getEnable();
        Long startTime = vo.getStartTime();
        Long endTime = vo.getEndTime();

        Wrapper<SysType> wrapper =
                Wrappers.lambdaQuery(SysType.class)
                        .eq(id != null, SysType::getId, id)
                        .like(!StringUtils.isEmpty(name), SysType::getName, name)
                        .like(!StringUtils.isEmpty(came), SysType::getCname, came)
                        .eq(status != null, SysType::isEnable, status)
                        .ge(startTime != null, SysType::getCtime, startTime)
                        .le(endTime != null, SysType::getCtime, endTime)
                        .orderByDesc(SysType::getCtime);

        PageHelper.startPage(vo.getPage(), vo.getPageSize());
        return new PageResult<>(mapper.selectList(wrapper));
    }
    
    public R add(SysType type) {
        if (isNameExist(type.getName(), null)) {
            return R.fail("添加失败，角色名称重复");
        }
        int rows = mapper.insert(type);
        return rows > 0 ? R.success("添加成功") : R.fail("添加失败");
    }
    
    public R update(SysType type) {
        Integer id = type.getId();
        String name = type.getName();

        if (isNameExist(name, id)) {
            return R.fail("修改失败，公告类型名称重复");
        }
        mapper.update(
                null,
                Wrappers.lambdaUpdate(SysType.class)
                        .set(SysType::getName, name)
                        .set(SysType::isEnable, type.isEnable())
                        .eq(SysType::getId, id)
        );
        return R.success("修改成功");
    }
    
    public R del(SysType type) {
        int rows = mapper.delete(
                Wrappers.lambdaQuery(SysType.class)
                        .eq(SysType::getId, type.getId())
                        .eq(SysType::isEnable, false)
        );
        return rows > 0 ? R.success("删除成功") : R.fail("删除失败，请刷新重试");
    }

    private boolean isNameExist(String name, Integer id) {
        Integer num = mapper.selectCount(
                Wrappers.lambdaQuery(SysType.class)
                        .eq(SysType::getName, name)
                        .ne(id != null, SysType::getId, id)
        );

        return num != null && num > 0;
    }
}
